package com.hoshiibuy.platform.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.platform.entity.PlatUsers;
import com.hoshiibuy.platform.entity.param.PlatUsersInsertParam;
import com.hoshiibuy.platform.entity.param.PlatUsersParam;
import com.hoshiibuy.platform.entity.param.PlatUsersUpdateParam;
import com.hoshiibuy.platform.service.PlatUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 用户基本信息表 前端控制器
 * </p>
 *
 * @author yl
 * @since 2019-04-20
 */
@Controller
@RequestMapping("/platUsers")
@Api(value = "平台用户管理", tags = {"平台用户信息操作接口"})
@Slf4j
public class PlatUsersController {

    @Autowired
    private PlatUsersService platUsersService;

    /**
     * @param param
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 分页查询平台用户信息
     */
    @ApiOperation(value = "分页查询平台用户记录")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid PlatUsersParam param) {
        try {

            QueryWrapper<PlatUsers> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(param.getName()))
                queryWrapper.lambda().eq(PlatUsers::getName, param.getName());

            if (param.getPlatid() != 0)
                queryWrapper.lambda().eq(PlatUsers::getPlatid, param.getPlatid());

            if (StringUtils.isNotBlank(param.getUsercode()))
                queryWrapper.lambda().eq(PlatUsers::getUsercode, param.getUsercode());


            IPage<PlatUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));

            return Response.data(platUsersService.page(page, queryWrapper));

        } catch (ServiceException e) {
            log.error("分页查询平台用户信息异常", e);
            return Response.failed(e.getError().getMsg());

        } catch (Exception e) {
            log.error("分页查询平台用户信息异常", e);
            return Response.failed("分页查询平台用户信息异常");

        }

    }

    /**
     * @param [id]
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据id查询平台用户信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询平台用户信息")
    public Response queryPlatUsersById(Long id) {
        return Response.data(platUsersService.getById(id));
    }

    /**
     * @param [platId]
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据平台id查询
     */
    @GetMapping("/query")
    @ApiOperation(value = "查询平台用户信息")
    public Response queryPlatformByName(Long platId) {

        Wrapper wrapper = Wrappers.<PlatUsers>query()
                .lambda().eq(PlatUsers::getPlatid, platId);

        return Response.data(platUsersService.getOne(wrapper));

    }

    /**
     * @param [platUsersInsertParam]
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台用户信息")
    public Response savePlatform(@Valid PlatUsersInsertParam platUsersInsertParam) {
        try {
            PlatUsers platUsers = BeanUtils.covertType(platUsersInsertParam, PlatUsers.class);
            return Response.data(platUsersService.save(platUsers));
        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }


    }

    /**
     * @param [platUsersUpdateParam]
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台用户信息")
    public Response updatePlatformById(@Valid PlatUsersUpdateParam platUsersUpdateParam) {
        try {
            PlatUsers platUsers = BeanUtils.covertType(platUsersUpdateParam, PlatUsers.class);
            return Response.data(platUsersService.updateById(platUsers));
        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }


    }
}

